package com.bdqn.easybuy1.service.product;

import com.bdqn.easybuy1.dao.ProductDao;
import com.bdqn.easybuy1.dao.impl.ProductDaoImpl;
import com.bdqn.easybuy1.entities.Product;
import com.bdqn.easybuy1.utils.JDBCUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.util.List;

public class productServiceImpl implements ProductService {
    @Override
    public boolean add(Product product) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            count = productDao.insert(connection, product);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public boolean update(Product product) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            count = productDao.update(connection, product);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public boolean deleteProductById(Integer productId) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            count = productDao.deleteProductById(connection, productId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public Product getProductById(Integer productId) {
        Connection connection = null;
        Product product = null;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            product = productDao.getProductById(connection, productId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return product;
        }
    }

    @Override
    public List<Product> getProductList(Integer currentPageNo, Integer pageSize, String proName, Integer categoryId, Integer level) {
        Connection connection = null;
        List<Product> productList = null;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            productList = productDao.getProductList(connection, currentPageNo, pageSize, proName, categoryId, level);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return productList;
        }
    }

    @Override
    public int count(String proName, Integer categoryId, Integer level) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            ProductDao productDao = new ProductDaoImpl();
            count = productDao.queryProductCount(connection, proName, categoryId, level);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count;
        }
    }

    @Override
    public boolean updateStock(Integer productId, Integer stock) {
        return false;
    }
}
